import * as url from '../../../http/api'
import axios from 'axios'
import http from '../../../http/http'

const state = {
    workOnTime: '',
    workOffTime: '',
    workHour: '',
    workMinutes: '',
    workOnDate: {},
    workOffDate: {},
    BaseSalary:0,
    Bonus:0,  //奖金
    Deduction:0, //扣罚
    workArrangeData:[],
    attendanceData:[],
    workStateDate:'',


}
const mutations = {
    getWorkOnTime(state, obj) {
        state.workOnTime = obj.goToTime.time;
        state.workOnDate = obj.goToTime.date;

    },
    getWorkOffTime(state, obj) {
        state.workOffTime = obj.goOffTime.time;
        state.workOffDate = obj.goOffTime.date;
    },
    getWorkHour(state) {
        state.workHour = Math.floor((state.workOffDate - state.workOnDate) / 1000 / 60 / 60);
        state.workMinutes = Math.round((state.workOffDate - state.workOnDate) / 1000 / 60 % 60);
    },
    ckeckSalaryInfo(state,obj){
        state.BaseSalary = obj.BaseSalary;
        state.Bonus = obj.Bonus;
        state.Deduction = obj.Deduction;

    },
    checkWorkArrange(state,obj){
        state.workArrangeData=obj.SchedulingList
    },
    checkAttendanceInfo(state,obj){
        state.attendanceData=obj.AttendanceList
    },
    workStateDataSelect(state,obj){
        state.workOnTime = obj.Attendance.goToTime;
        let dateOnStr =obj.Attendance.goToTime.replace(/-/g,'/');
        state.workOnDate=new Date(dateOnStr);
        state.workOffTime = obj.Attendance.goOffTime;
        let dateOffStr =obj.Attendance.goOffTime.replace(/-/g,'/');
        state.workOffDate=new Date(dateOffStr);
    },

}
const actions = {
    /*排班分页查询*/
    getWorkStatetAct({commit}, obj) {
        console.log('getWorkStatetAct');

        return http({
            url: url.getStaffWorkState,
            method: 'post',
            data: obj

        }).then(resp => {
            console.log(resp.data);
            if (resp.data.Code === 200) {
                commit('workStateDataSelect',resp.data)
                commit('getWorkHour')
            }
            return resp.data
        }).catch(err => {
            console.log(err);
        })
    },


    getWorkOnTimeAct({commit},obj) {
        console.log('getWorkOnTimeAct');

        return http({
            url:url.staffOnWork,
            method:'post',
            data:{
                staffId:obj.staffId,
                goToTime:obj.goToTime.time
            },
        }).then(resp=>{
            console.log(resp);
            if (resp.data.Code === 200) {
                commit('getWorkOnTime', obj)
            }

            return resp.data

        }).catch(err=>{
            console.log(err);
        })
    },

    getWorkOffTimeAct({commit},obj) {
        console.log('getWorkOffTimeAct');

        return http({
            url:url.staffAfterWork,
            method:'post',
            data:{
                staffId:obj.staffId,
                goOffTime:obj.goOffTime.time
            },
        }).then(resp=>{
            console.log(resp);
            if (resp.data.Code === 200) {

                commit('getWorkOffTime', obj)
                commit('getWorkHour')
            }
            return resp.data

        }).catch(err=>{
            console.log(err);
        })
    },
    ckeckSalaryInfoAct({commit}, obj){
        console.log('ckeckSalaryInfoAct');
        http({
            url:url.staffWage,
            method:'post',
            data:obj
        }).then(resp=>{
            // console.log(resp.data);
            if (resp.data.Code === 200) {
                commit('ckeckSalaryInfo',resp.data)
            }
        }).catch(err=>{
            console.log(err);
        })
    },
    checkWorkArrangeAct({commit}, obj){
        console.log('checkWorkArrangeAct');
        return http({
            url:url.staffScheduling,
            method:'post',
            data:obj
        }).then(resp=>{
            console.log(resp.data);
            if (resp.data.Code === 200) {
            commit('checkWorkArrange',resp.data);
            }
        }).catch(err=>{
            console.log(err);
        })
    },
    checkAttendanceInfoAct({commit}, obj){
        console.log('checkAttendanceInfoAct');

        return http({
            url:url.staffAttendance,
            method:'post',
            data:obj
        }).then(resp=>{
            console.log(resp.data);
            if (resp.data.Code === 200) {
                commit('checkAttendanceInfo',resp.data);
            }
        }).catch(err=>{
            console.log(err);
        })
    },

}

export default {
    state,
    actions,
    mutations,

}
